﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace EBA.Tests
{
    class ConfigHelper : IDisposable
    {
        public string FileName { get; set; }

        #region Dispose
        private bool _Disposed = false;
        /// <summary>
        /// 析构函数，释放资源。
        /// </summary>
        ~ConfigHelper()
        {
            Dispose(false);
        }

        /// <summary>
        /// 手动释放资源。
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }

        /// <summary>
        /// 手动释放资源。
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!_Disposed)
            {
                if (disposing)
                {
                    File.Delete(this.FileName);
                    GC.SuppressFinalize(this);
                }
                _Disposed = true;
            }
        }
        #endregion


        public static ConfigHelper Create()
        {
            var configFile = Path.Combine(AppRuntime.ApplicationPath, Guid.NewGuid().ToString("N") + ".config");

            using (var writer = File.CreateText(configFile))
            {
                writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                writer.WriteLine("<configuration>");
                writer.WriteLine("</configuration>");
                writer.Flush();
            }
            return new ConfigHelper() { FileName = configFile };
        }
    }
}
